//
//  HJDNavigationController.m
//  JYHospital
//
//  Created by 胡金东 on 16/3/31.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDNavigationController.h"


@interface HJDNavigationController ()<UINavigationControllerDelegate>

@property (nonatomic,strong) id popDelegate;

@end

@implementation HJDNavigationController

+(void)initialize
{
    //获取当前类下面的UIBarButtonItem
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    
    //设置导航条按钮的颜色
    NSMutableDictionary *titleColor = [NSMutableDictionary dictionary];
    titleColor[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [item setTitleTextAttributes:titleColor forState:UIControlStateNormal];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //处理滑动返回的bug，在-(void)navigationController:中判断一下
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
    
    
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}




//导航控制器跳转完成的时候调用
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if(viewController == self.viewControllers[0]){//显示根控制器
        //还原滑动返回手势的代理
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
        
    }else {//不是显示根控制器
        //实现滑动返回功能
        //实现滑动返回功能，清空滑动返回手势的代理，就能实现滑动功能
        self.interactivePopGestureRecognizer.delegate = nil;
        
    }
}


-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //    NSLog(@"%ld",self.viewControllers.count);
    //    NSLog(@"%s",__func__);
    //设置费根控制器的导航条的内容
    if (self.viewControllers.count != 0) {//等于0说明是根控制器
        
        viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem *left = [UIBarButtonItem itemWithTarget:self action:@selector(popToPre) image:@"︿" HighImage:nil];
        viewController.navigationItem.leftBarButtonItem = left;
        
        UIBarButtonItem *right = [UIBarButtonItem itemWithTarget:self action:@selector(popToRoot) image:@"navigationbar_more" HighImage:@"navigationbar_more_highlighted"];
        viewController.navigationItem.rightBarButtonItem = right;
        
    }
    [super pushViewController:viewController animated:animated];
}

-(void)popToPre
{
    [self popViewControllerAnimated:YES];
}

-(void)popToRoot
{
    [self popToRootViewControllerAnimated:YES];
}
@end
